#!/bin/bash

# Alias: TCP host address for Apache
# Menu: Web GUI
# Description:
#  Configure the TCP host address for the Apache webserver
#  process of this site.
#
#  It might be useful to change the host address the site
#  Apache webserver listes to.
#
#  After changing this variable, the man Apache webserver
#  must be restarted.

# Load other config options. This hook needs
# APACHE_TCP_PORT.
if [ -f "$OMD_ROOT/etc/omd/site.conf" ]; then
    . "$OMD_ROOT/etc/omd/site.conf"
else
    CONFIG_APACHE_TCP_PORT=${CONFIG_APACHE_TCP_PORT:-0}
fi

case "$1" in
default)
    echo "127.0.0.1"
    ;;
choices)
    echo "@{APACHE_TCP_ADDR}"
    ;;
set)
    APACHE_HOST=$2
    if [[ "$APACHE_HOST" == \[* ]]; then
        cat <<EOF >"$OMD_ROOT/etc/apache/listen-port.conf"
# This file is managed by 'omd config set APACHE_TCP_PORT' and 'omd config set APACHE_TCP_ADDR'.
# Better do not edit manually
Listen $APACHE_HOST:$CONFIG_APACHE_TCP_PORT
EOF
    else
        cat <<EOF >"$OMD_ROOT/etc/apache/listen-port.conf"
# This file is managed by 'omd config set APACHE_TCP_PORT' and 'omd config set APACHE_TCP_ADDR'.
# Better do not edit manually
Listen $APACHE_HOST:$CONFIG_APACHE_TCP_PORT
ServerName $APACHE_HOST:$CONFIG_APACHE_TCP_PORT
EOF
    fi
    ;;
depends)
    [ "$CONFIG_APACHE_MODE" = own ]
    ;;
esac
